<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
use \Common\Model\Goods;
use \Common\Model\GoodsAttr;
use \Common\Model\TypeAttr;
use \Common\Model\GoodsList;
class ShopController extends Controller{
	public function index(){
		$gid=Q('get.gid',0,'intval');
		$goodsModel=new Goods();
		$goodsData=$goodsModel->where("gid=$gid")->find();
		// 找到gid对应的商品属性
		$goodsAttrModel=new GoodsAttr();
		$goodsAttrData=$goodsAttrModel
					  ->join("type_attr","type_attr_taid",'=','taid')
					  ->where("goods_gid=$gid AND class=1")->get();
		// p($goodsAttrData);
		$temp=[];
		foreach ($goodsAttrData as $k => $v) {
			$temp[$v['type_attr_taid']][]=$v;
		}
		// p($temp);
		// 找到类型属性对应的类型名字以及值对应起来
		$TypeAttrModel=new TypeAttr();
		// 组合名字和属性值
		$conAttr=[];
		
		foreach ($temp as $k => $v) {
			$conAttr[]=[
				'name'=>$TypeAttrModel->where("taid={$k} AND class=1")->pluck('taname'),
				'value'=>$v
				];
		}
		// p($conAttr);
		View::with('conAttr',$conAttr);
		View::with('goodsData',$goodsData);
		View::make();
	}
	public function getAttr(){
		$combo=Q('post.combo');
		$combo=rtrim($combo,',');
		$GoodsListModel=new GoodsList;
		$data=$GoodsListModel->where("combo = '{$combo}'")->find();
		if($data){
			echo json_encode($data);exit;
		}
		echo json_encode(0);exit;
	}
}